package com.gitee.wsl.compose.ext.ui

import androidx.compose.foundation.pager.PagerState
import androidx.compose.runtime.Stable

/**
 * Calculate absolute offset for current page [page].
 * * For previous page, this will be -1
 * * For current, 0
 * * For next, 1
 * * For one after the next - 2
 *
 * and so on...
 */
@Stable
 fun PagerState.offsetForPage(page: Int): Float = (currentPage - page + currentPageOffsetFraction) * -1


/**
 * Same behavior as [LazyListState.firstVisibleItemIndex].
 */
 inline val PagerState.firstVisiblePage: Int
    get() = when {
        currentPageOffsetFraction >= 0 -> currentPage
        else -> currentPage - 1
    }.coerceIn(0..pageCount)

/**
 * Same behavior as [LazyListState.firstVisibleItemScrollOffset].
 */
 inline val PagerState.firstVisiblePageOffsetFraction: Float
    get() = getOffsetDistanceInPages(firstVisiblePage.coerceIn(0..pageCount))